

import java.io.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

public class Knn {

    static class Example {
	public List<Integer> observation;
	public Integer label;

	public Example(List<Integer> obser, Integer label) {
	    this.observation = obser;
	    this.label = label;
	}

	public String toString() {
	    return "" + this.label;
	}
    }

    public static List<Example> readData(String fileName) 
	throws IOException, FileNotFoundException {
		
	List<Example> res = new ArrayList<Example>();
	InputStream ips = new FileInputStream(fileName); 
	InputStreamReader ipsr = new InputStreamReader(ips);
	BufferedReader br = new BufferedReader(ipsr);

	while (true) {
	    String line = br.readLine();
	    if (line == null) {
		break;
	    }
	    
	    String[] el = line.split(" ");
	    List<Integer> obser = new ArrayList<Integer>();
	    for (int i = 0; i < el.length - 1; i++) {
		obser.add(Integer.parseInt(el[i]));
	    }
	    Integer label = Integer.parseInt(el[el.length - 1]);

	    Example e = new Example(obser, label);
	    res.add(e);
	}
	br.close(); 

	return res;
    }		
    
    public static double knnEuclideanDistance(Example e1, Example e2) {
    	double acc = 0;
    	for(int i=0;i<e1.observation.size();i++) {
    		acc += Math.pow((e2.observation.get(i) - e1.observation.get(i)), 2);
    	}
    	double dEuc = Math.sqrt(acc);
    	return dEuc;
    }
    
    private static int plusFreq(Hashtable<Example,Double> kp) {
    	int c1 = 0;
    	int c2 = 0;
    	int c3 = 0;
    	int c4 = 0;
    	int c5 = 0;
    	int c6 = 0;
    	int c7 = 0;
    	String res = "";
    	for(Example e : kp.keySet()) {
    		switch(e.label) {
    		case 1 : c1++;
    		case 2 : c2++;
    		case 3 : c3++;
    		case 4 : c4++;
    		case 5 : c5++;
    		case 6 : c6++;
    		case 7 : c7++;
    		}
    	}
    	return Math.max(c1, Math.max(c2, Math.max(c3, Math.max(c4, Math.max(c5, Math.max(c6, c7))))));
   /* 	if(cmax == c1) {
    		res = "red soil";
    	}
    	if(cmax == c2) {
    		res = "cotton crop";
    	}
    	if(cmax == c3) {
    		res = "grey soil";
    	}
    	if(cmax == c4) {
    		res = "damp grey soil";
    	}
    	if(cmax == c5) {
    		res = "soil with vegetation stubble";
    	}
    	if(cmax == c6) {
    		res = "mixture class";
    	}
    	if(cmax == c7) {
    		res = "very damp grey soil";
    	}*/
    }
    
    
    // Question 3
    public static int classeExempleKppv(String fCorpus, Example ex, int k) throws FileNotFoundException, IOException {
    	List<Example> corpus;
		corpus = readData(fCorpus);
		Hashtable<Double,Example> dEcl = new Hashtable<Double,Example>();
		
    	List<Double> u = new ArrayList<Double>();
    	for(Example d : corpus) {
    		double dist = knnEuclideanDistance(d, ex);
    		dEcl.put(dist, d);
    		u.add(dist);
    	}
    	Collections.sort(u);
    	List<Double> kPremDist = u.subList(0, k-1);
    	Hashtable<Example,Double> kPrem = new Hashtable<Example,Double>();
    	for(int i=0;i<k-1;i++) {
    		kPrem.put(dEcl.get(kPremDist.get(i)), kPremDist.get(i));
    	}
    	return plusFreq(kPrem);
    }
 
    public static void main(String[] v) throws IOException, FileNotFoundException {
	/*for (Example e : readData("sat.tst")) {
	    System.out.println(e.observation);
	}*/
    	Example ex = readData("test2").get(0);
    	System.out.println(classeExempleKppv("test1",ex,3));
    }
}